package com.kx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kx.bean.Car;
import com.kx.bean.User;
import com.kx.dao.CarMapper;
import com.kx.dao.UserMapper;
import com.kx.service.UserService;
import com.kx.vo.DataVO;
import com.kx.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CarMapper carMapper;

    @Override
    public DataVO<UserVO> findData(Integer page, Integer limit) {
        DataVO<UserVO> dataVO = new DataVO<>();
        //1、设置code
        dataVO.setCode(0);
        //2、设置msg
        dataVO.setMsg("");
        //3、设置count(如果使用了mybatis-plus分页，则不需要调用其中selectCount方法单独调用)
//        dataVO.setCount(UserMapper.selectCount(null));
//        List<User> UserList= UserMapper.selectList(null);
        //取消上方全查询，分页设置List<User>通过getRecords()获取
        IPage<User> userIPage = new Page<>(page, limit);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("state", 0);
        IPage<User> result = userMapper.selectPage(userIPage, wrapper);
        dataVO.setCount(result.getTotal());

        List<User> userList = result.getRecords();
        System.out.println("获取到数据库的真实用户数据：" + userList);

        List<UserVO> userVOList = new ArrayList<>();

        for (User user : userList) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user, userVO);

//            if (user.getCid()!=null){
            QueryWrapper<Car> carQueryWrapper = new QueryWrapper<>();
            carQueryWrapper.eq("cid", user.getCid());
            System.out.println(user.getCid());
            if (user.getCid() != null && user.getCid()!=0) {
                Car car = carMapper.selectOne(carQueryWrapper);
                userVO.setCph(car.getLicensePlate());
            } else {
                userVO.setCph("");
            }

//            }
            userVOList.add(userVO);
        }
        System.out.println("前端显示的用户数据：" + userVOList);
//            //如果上方代码种类1报了空指针异常，则数据库没有此数据，需要进行if(xx!=null)，才能正确获取到name，种类2、3也可仿1改
//            QueryWrapper<UserCategory> queryWrapper1 = new QueryWrapper<>();
//            queryWrapper1.eq("id", User.getCategoryleveloneId());
//            UserCategory UserCategory1 = UserCategoryMapper.selectOne(queryWrapper1);
//            if (UserCategory1 != null) {
//                userVO.setCategorylevelone(UserCategory1.getName());
//            }
        //4、设置data
        dataVO.setData(userVOList);
        return dataVO;
    }

    @Override
    public DataVO<User> findBlackData(Integer page, Integer limit) {
        DataVO<User> dataVO = new DataVO<>();
        //1、设置code
        dataVO.setCode(0);
        //2、设置msg
        dataVO.setMsg("");
        IPage<User> userIPage = new Page<>(page, limit);
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.eq("in_out_status", 0).eq("payment_status", "未支付").ne("packing_fee", 0);
        List<Car> carList = carMapper.selectList(wrapper);
        System.out.println("======================carList==============================" + carList);
        QueryWrapper<User> wrapper1 = new QueryWrapper<>();
        Map<String, Integer> maps = new HashMap<>();
        for (Car car : carList) {
            maps.put("cid", car.getCid());
        }
        System.out.println("======================maps==============================" + maps);
        wrapper1.allEq(maps);

        IPage<User> result = userMapper.selectPage(userIPage, wrapper1);
        dataVO.setCount(result.getTotal());
        List<User> userList1 = result.getRecords();
        dataVO.setData(userList1);
        return dataVO;
    }

    @Override
    public List<User> findBlackData2() {
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.eq("in_out_status", 0).ne("packing_fee", 0).eq("payment_status", "未支付");
        List<Car> carList = carMapper.selectList(wrapper);
        System.out.println("======================carList==============================" + carList);
        QueryWrapper<User> wrapper1 = new QueryWrapper<>();
        Map<String, Integer> maps = new HashMap<>();
        for (Car car : carList) {
            maps.put("cid", car.getCid());
        }
        wrapper1.allEq(maps);
        List<User> userList1 = userMapper.selectList(wrapper1);
        return userList1;
    }

    @Override
    public User queryUserByName(String username) {
        return userMapper.queryUserByName(username);
    }

    @Override
    public User selectByUsernameAndPassword(String username, String password) {
        return userMapper.selectByUsernameAndPassword(username, password);
    }

    @Override
    public User findById(Integer uid) {
        return userMapper.selectByPrimaryKey(uid);
    }

    @Override
    public User findByCid(Integer cid) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid", cid);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public int updateUser(User user) {
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("uid", user.getUid());
        return userMapper.update(user, wrapper);
    }

    @Override
    public int updateByPrimaryKeySelective(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 停用，假的删除
     *
     * @param uid
     * @return
     */
    @Override
    public int deleteUser(int uid) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        User user = userMapper.selectById(uid);
        user.setState(1);
//        System.out.println("==============="+user);
        return userMapper.update(user, queryWrapper);
    }

    @Override
    public int saveUser(User user) {
        return userMapper.insert(user);
    }

}
